<?php

class Ynbusinesspages_Api_ConvertMailVars extends Core_Api_Abstract 
{
	
	protected static $_baseUrl;
	
	public static function getBaseUrl()
	{
		$request =  Zend_Controller_Front::getInstance()->getRequest();
		if(self::$_baseUrl == NULL && $request)
		{
			self::$_baseUrl = sprintf('%s://%s', $request->getScheme(), $request->getHttpHost());
			
		}
		return self::$_baseUrl;
	}
	/**
	 * @param   string $type
	 * @return  string
	 */
	public function selfURL() 
    {
      return self::getBaseUrl();
    }

	public function inflect($type) {
		return sprintf('vars_%s', $type);
	}

	public function vars_default($params, $vars) {
		return $params;
	}

	/**
	 * call from api
	 */
	public function process($params, $vars, $type) {
		$method_name = $this->inflect($type);
		if(method_exists($this, $method_name)) {
			return $this -> {$method_name}($params, $vars);
		}
		return $this -> vars_default($params, $vars);
	}

	/**
	 *
	 */
	public function vars_groupbuy_buygiftseller($params, $vars) {
		$rparams = array();
		$deal = Engine_Api::_()->getItem('deal', $params['item_id']);  
		$rparams['deal_title'] = "\"".$deal->title."\"";
		$url = Engine_Api::_()->getApi('settings','core')->getSetting('groupbuy.baseUrl','http://');
		$rparams['deal_link'] = $this->getBaseUrl().$deal->deal_href;
		$rparams['deal_price'] = number_format($deal->final_price,2)." ".$deal->currency;
		$rparams['deal_quantity'] = $params['number'];
		$rparams['deal_total'] = number_format($params['amount'],2)." ".$deal->currency;
		$buyer = Engine_Api::_()->getItem('user', $params['user_id']);
		$rparams['buyer_name'] = $buyer->displayname;
		$rparams['buyer_email'] = $buyer->email;
		$Bills  =  new Groupbuy_Model_DbTable_Bills;
		$select =  $Bills->select()->where('bill_id=?',$params['bill_id'] );
		$bill =  $Bills->fetchRow($select);
		$gift =  $bill->getGift();	
		$rparams['buyer_note'] = $gift->note;
		$rparams['friend_name'] = $gift->friend_name;
		$rparams['friend_email'] = $gift->friend_email;
		$rparams['friend_address'] = $gift->friend_address;
		$rparams['friend_phone'] = $gift->friend_phone;
		$rparams['deal_tranid'] = $params['code'];	
		$rparams['deal_code'] = $params['coupon_codes'];
		return $rparams;
	}
}


